#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "agvmodule_defines.h"
#include <QEvent>
#include <QMainWindow>
#include <opencv2/core/mat.hpp>
#include <qglobal.h>
#include <qtcpsocket.h>

#include "AGVThreadController.h"

#include "reflection/jsonization.hpp"
#include "src/LogListPanel/LogListPanel.h"
#include "third_party/agv_imu_requester/AGVRequester.h"

#include "struct.h"
#include "third_party/mtcp-read-write/src/MTcpReadWriteController.h"

#include "LogListPanel/LogListPanel.h"
#include "TestPicShowWidget.h"

#include "CustomEvent.h"

#include "opencv2/opencv.hpp"

namespace Ui {
class MainWindow;
}

struct ShowPicEventData {
  int window_num{1};
  cv::Mat mat;
};

using ShowPicEvent = AGVModuleAbstractEvent<ShowPicEventData, QT_USER_EVENT_SHOWPIC>;

// class ShowPicEvent
//     : public AGVModuleAbstractEvent<cv::Mat, QT_USER_EVENT_SHOWPIC> {
//   public:
//   ShowPicEvent(const cv::Mat &data, int window_num)
//       : AGVModuleAbstractEvent(data), window_num_(window_num) {}

//   auto window_num() const { return window_num_; }

//   private:
//   int window_num_{1};
// };

struct agv_return_msg_locate_t {
  double x{0.0};
  double y{0.0};
  double angle{0.0};
  double confidence{0.0};
  int ret_code{-1};
  std::string create_on{"unknown"};

  MEO_JSONIZATION(MEO_OPT x, MEO_OPT y, MEO_OPT angle, MEO_OPT confidence,
                  MEO_OPT ret_code, MEO_OPT create_on);
};

class MainWindow : public QMainWindow {
  Q_OBJECT

  public:
  explicit MainWindow(QWidget *parent = nullptr);
  ~MainWindow();

  public slots:
  int send_frame_to_upper(const MTcpFrame &frame);

  private:
  void _init_agv_signal_slots();
  void _init_test_buttons();

  void _process_frame(const MTcpFrame &frame);

  void _request_locate();

  void closeEvent(QCloseEvent *event) override;
  void customEvent(QEvent *e) override;

  static QString TcpStateToString(QTcpSocket::SocketState s);

  static QString AGVStateToString(State agv_state);
  static QString AGVMoveStepToString(AGVMoveStep agv_move_step);

  private:
  Ui::MainWindow *ui;
  LogListPanel *log_list_panel_;
  TestPicShowWidget *test_pic_show_widget_;

  MTcpReadWriteController *tcp_rw_ctrler_;
  QTimer *info_return_timer_;

  AGVThreadController *agv_thread_ctrler_;

  AGVRequester *agv_requester_;

  agv_return_msg_locate_t cached_agv_rtn_locate_data_;
  QTimer *request_locate_timer_;
};

#endif // MAINWINDOW_H
